import router from "@/router";
import Layout from "@/layout/Layout";
import UserLayout from "@/layout/user/Layout.vue";

// 设置动态路由的方法，用户登录成功后，获取角色权限信息，在这里解析
// permissions是一个资源的数组
export function activeUserRouter() {
    const userStr = sessionStorage.getItem("user");
    if (userStr) {
      const user = JSON.parse(userStr);
      let root = {
        path: "/user",
        name: "UserLayout",
        component: UserLayout,
        redirect: "/user/home",
        children: [],
      };
      //动态处理权限路径路由
      user.permissions.forEach((p) => {
        
        if (p.path === "/postExamManage") {
          p.path = "/user/postExamManage";
        }
        
        let obj = {
          path: p.path,
          name: "User"+p.permissionName,
          component: () => import("@/views/user/" + p.permissionName),
        };
        root.children.push(obj);
      });
      if (router) {
        router.addRoute(root);
      }
    }
}


// 设置动态路由的方法，用户登录成功后，获取角色权限信息，在这里解析
// permissions是一个资源的数组
export function activeRouter() {
    const userStr = sessionStorage.getItem("user")
    if (userStr) {
        const user = JSON.parse(userStr)
        let root = {
            path: '/',
            name: 'Layout',
            component: Layout,
            redirect: "/home",
            children: []
        }
        //动态处理权限路径路由
        user.permissions.forEach(p => {
            let obj = {
              path: p.path,
              name: p.permissionName,
              component: () => import("@/views/" + p.permissionName),
            };
            root.children.push(obj)
        })
        if (router) {
            router.addRoute(root)
        }
    }
}


